using System;
using System.Collections.Generic;
using EnvDTE;
using NDomain.Helpers;
using NDomain.Reflection;
using System.Collections;

namespace NDomain.DomainExplorer
{
    internal class DTE2ProjectInfoProvider : IProjectInfoProvider
    {
        private Project _project;
        private List<DTE2ClassInfoProvider> _classes;

        public DTE2ProjectInfoProvider(Project project)
        {
            _project = project;
        }

        public string GetProjectName()
        {
            return _project.Name;
        }

        public ICollection<IClassInfoProvider> GetClasses()
        {
            if (_classes == null)
            {
                _classes = new List<DTE2ClassInfoProvider>();
                LoadClasses(_project.ProjectItems);
            }

            return _classes.ToArray();
        }

        private void LoadClasses(ProjectItems items)
        {
            foreach (ProjectItem item in items)
            {
                if (item.Kind == Constants.vsProjectItemKindPhysicalFolder)
                    LoadClasses(item.ProjectItems);

                if (item.Kind == Constants.vsProjectItemKindPhysicalFile && item.FileCodeModel != null)
                    foreach (CodeNamespace codeNamespace in IEnumerableHelper.Filter<CodeNamespace>(item.FileCodeModel.CodeElements))
                        foreach (CodeClass codeClass in IEnumerableHelper.Filter<CodeClass>(codeNamespace.Children))
                            _classes.Add(new DTE2ClassInfoProvider(codeNamespace, codeClass));
            }
        }
    }
}